package com.corpcredit.webapp.action;

import java.io.Serializable;
import java.util.Date;

import com.corpcredit.Constants;
import com.corpcredit.model.Cbtdqk;
import com.corpcredit.model.Jbqk;
import com.corpcredit.webapp.action.BasePage;
import com.corpcredit.service.GenericManager;

public class CbtdqkForm extends BasePage implements Serializable {
    private GenericManager<Cbtdqk, Long> cbtdqkManager;
    private Cbtdqk cbtdqk = new Cbtdqk();
    private Long id;
    
    private Jbqk jbqk;

    public void setJbqk( Jbqk jbqk )
    {
        this.jbqk = jbqk;
    }

    public void setCbtdqkManager(GenericManager<Cbtdqk, Long> cbtdqkManager) {
        this.cbtdqkManager = cbtdqkManager;
    }

    public Cbtdqk getCbtdqk() {
        return cbtdqk;
    }

    public void setCbtdqk(Cbtdqk cbtdqk) {
        this.cbtdqk = cbtdqk;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String delete() {
        cbtdqkManager.remove(cbtdqk.getId());
        addMessage("cbtdqk.deleted");

        return "list";
    }

    public String edit() {
        // Comparison to zero (vs. null) is required with MyFaces 1.2.2, not with previous versions
        if (id != null && id != 0) {
            cbtdqk = cbtdqkManager.get(id);
        } else {
            cbtdqk = new Cbtdqk();
        }

        return "edit";
    }

    public String save() {
        boolean isNew = (cbtdqk.getId() == null || cbtdqk.getId() == 0);
        cbtdqk.setJbqk( jbqk );
        // set username who act this operation, get the user name from httprequest
        cbtdqk.setUsername( getRequest().getRemoteUser() );
        // set operation date using the current time
        cbtdqk.setOptdate( new Date() );
        // set the operation type, default two types: add and update
        cbtdqk.setOperation( (isNew) ? Constants.OPERATION_ADD : Constants.OPERATION_UPDATE );
       
        cbtdqkManager.save(cbtdqk);

        String key = (isNew) ? "cbtdqk.added" : "cbtdqk.updated";
        addMessage(key);

        if (isNew) {
            return "list";
        } else {
        	 // set the id to the next page
            this.id = cbtdqk.getId();
            // return the update confirmation page
            return edit();
        }
    }
} 